package com.boss.salary.dao.impl;

import com.boss.common.util.DateUtils;
import com.boss.framework.dao.BaseDaoImpl;
import com.boss.salary.constant.SystemDictConstant;
import com.boss.salary.constant.TableName;
import com.boss.salary.dao.IExportBankTypeDao;
import com.boss.salary.entity.ExportBankType;
import com.boss.sys.ca.user.dto.UserDTO;
import com.boss.sys.utils.SysUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class ExportBankTypeDaoImpl extends BaseDaoImpl implements IExportBankTypeDao {
    /**
     * 保存前删除当前银行存入的数据
     * @param bankName
     * @param province
     */
    @Override
    public void DeleteExportBankType(String bankName, String province) {
        StringBuilder sql = new StringBuilder();
        sql.append(" delete from ").append(TableName.BACK_EXPORT);
        sql.append(" where bank_type=? and mof_div_code=?");
        this.getJdbcTemplate().update(sql.toString(),bankName,province);

    }

    /**
     * 存入选择银行要导出的内容
     * @param exportBankTypesList
     * @param userDTO
     */
    @Override
    public void saveExportBankType(List<ExportBankType> exportBankTypesList, UserDTO userDTO) {
        StringBuilder sql = new StringBuilder();
        sql.append("insert into ").append(TableName.BACK_EXPORT);
        sql.append("( id, create_time, salary_Id, salary_Name,")
                .append("column_name, mof_div_code, bank_type, sort, is_deleted) ")
                .append(" VALUES (?,?,?,?,?,?,?,?,?)");
        List<Object[]> paramsList = new ArrayList<>(exportBankTypesList.size());
        for (ExportBankType exportBankType : exportBankTypesList) {
            Object[] params = new Object[]{
                    SysUtils.generateGuid(),String.valueOf(DateUtils.getCurrentTimeStamp()),exportBankType.getSalaryId(),exportBankType.getSalaryName(),
                    exportBankType.getColumnName(),userDTO.getProvince(),exportBankType.getBankType(),exportBankType.getSort(),SystemDictConstant.IS_FALSE
            };
            paramsList.add(params);
        }
        super.getJdbcTemplate().batchUpdate(sql.toString(), paramsList);
    }

    @Override
    public List<ExportBankType> getExportBankList(String province) {
        StringBuilder sql = new StringBuilder();
        sql.append(" select id,mof_div_code,bank_type,column_name,salary_name,sort  from ").append(TableName.BACK_EXPORT).append(" where is_deleted = 0 ")
                .append("  and mof_div_code = ?  ")
                .append(" order by bank_type , sort asc ");
        return this.getjdbcTemplate().query(sql.toString(), new BeanPropertyRowMapper(ExportBankType.class),province);

    }
    @Override
    public List<ExportBankType> getInfoByPersonTypes(String bankCode, String mof_div_code) {

        StringBuilder sql = new StringBuilder();
        sql.append("select  ")
                .append(" distinct " )
                .append("spts.salary_id,spts.column_name,spts.salary_name,")
                .append("spts.bank_type,spts.sort from ").append(TableName.BACK_EXPORT).append(" spts ")
                .append(" join ").append(TableName.SALARY_BASEDATA).append(" sb ")
                .append(" on spts.mof_div_code=sb.mof_div_code ")
                .append(" where spts.mof_div_code=? ")
                .append(" and spts.is_deleted=0 and sb.base_state = '1' and sb.is_deleted=0 and spts.bank_type=? ");

        sql.append(" order by spts.bank_type , spts.sort asc");

        return this.getJdbcTemplate().query(sql.toString(), new BeanPropertyRowMapper<>(ExportBankType.class),mof_div_code,bankCode);
    }


}
